package com.eleven.trades.serverimpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.eleven.trades.entity.User;
import com.eleven.trades.error.BusinessException;
import com.eleven.trades.mapper.UserMapper;
import com.eleven.trades.server.LoginAndExitServer;
import com.eleven.trades.utils.CommonUtils;
import com.eleven.trades.vo.UserVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class LoginAndExitServerImpl implements LoginAndExitServer {

    @Autowired
    private UserMapper userMapper;

    @Override
    public int login(UserVo userVo) throws BusinessException {
        //对密码加密
        String password = CommonUtils.md5(userVo.getUserPassword());
        //todo 判断是手机登录还是身份证登录；
        //手机号进行登录
//        User user =  userMapper.selectByUserPhone(userVo.getUserPhone());
        QueryWrapper<User> qw = new QueryWrapper<>();
        qw.lambda().eq(User::getUserPhone,userVo.getUserPhone());
        User user = userMapper.selectOne(qw);
        if(user==null)
        {
            return -1;
//            throw new BusinessException(ResultCode.USER_ACCOUNT_NOT_EXIST,"用户手机号未注册");
        }
        //密码比对
        String target_password = user.getUserPassword();
        if(!StringUtils.equals(password,target_password))
        {
//            throw new BusinessException(ResultCode.USER_CREDENTIALS_ERROR,"用户密码错误");
            return 0;
        }
        return user.getUserId();

    }

    @Override
    public void exit() {

    }
}
